package curves.gui;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import curves.Curve;
import curves.math.ChaikinCurveAlgorithm;
import curves.sys.SimpleCurve;

public class MainRighClickMenu extends AbstractSurfaceMenu {

    private StepChooserDialog dialog;
    private JMenuItem lineShower;
    private JMenuItem dotShower;

    public MainRighClickMenu(Window frame) {
        super(frame);
        JMenuItem menuItem = new JMenuItem("Add point");
        menuItem.addActionListener(new AddPointAction());
        add(menuItem);
        addSeparator();

        menuItem = new JMenuItem("Set steps count");
        menuItem.addActionListener(new ChangeStepAction());
        add(menuItem);


        lineShower = new JMenuItem("Hide lines");
        lineShower.addActionListener(new ShowLinesAction());
        add(lineShower);


        dotShower = new JMenuItem("Show mini-dots");
        dotShower.addActionListener(new ChangeShowDots());
        add(dotShower);

        addSeparator();

        menuItem = new JMenuItem("New curve");
        menuItem.setToolTipText("Will create a new curve to put points to");
        menuItem.addActionListener(new AddCurveAction());
        add(menuItem);

        addSeparator();

        menuItem = new JMenuItem("Clear curves");
        menuItem.setToolTipText("Removes all curves");
        menuItem.addActionListener(new ClearCurvesAction());
        add(menuItem);
    }

    class AddPointAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if(getSurface().getCurrentCurve() == null){
                Curve curve = new SimpleCurve(getSurface(), new ChaikinCurveAlgorithm());
                getSurface().addCurve(curve);
                getSurface().setCurrentCurve(curve);
            }
            getSurface().getCurrentCurve().addPoint(getDestX(), getDestY());
            frame.getGraphicEngine().repaint();
        }
    }

    class AddCurveAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            getSurface().addCurve(new SimpleCurve(getSurface(), new ChaikinCurveAlgorithm()));
            getSurface().setCurrentCurve(getSurface().getCurves().get(getSurface().getCurves().size() - 1));
            getSurface().getCurrentCurve().addPoint(getDestX(), getDestY());
            frame.getGraphicEngine().repaint();
        }
    }

    class ClearCurvesAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            getSurface().getCurves().clear();
            getSurface().setCurrentCurve(null);
            frame.getGraphicEngine().repaint();
        }
        
    }

    class ShowLinesAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (getSurface().getShowLines()) {
                getSurface().setShowLines(false);
                lineShower.setText("Show lines");
            } else {
                getSurface().setShowLines(true);
                lineShower.setText("Hide lines");
            }
            frame.getGraphicEngine().repaint();
        }
    }

    class ChangeStepAction extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (dialog == null) {
                dialog = new StepChooserDialog(frame, getSurface().getSteps(), getSurface());
            }
            Point location = MouseInfo.getPointerInfo().getLocation();
            location.setLocation(location.getX() - dialog.getWidth() / 2.0, location.getY() - dialog.getHeight() / 2.0);
            dialog.setLocation(location);
            dialog.setVisible(true);

        }
    }

    class ChangeShowDots extends AbstractAction {

        public void actionPerformed(ActionEvent e) {
            if (getSurface().getShowDots()) {
                getSurface().setShowDots(false);
                dotShower.setText("Show mini-dots");
            } else {
                getSurface().setShowDots(true);
                dotShower.setText("Hide mini-dots");
            }
            frame.getGraphicEngine().repaint();
        }
    }
}
